#ifndef _NE_THREAD_H_
#define _NE_THREAD_H_

#include <system/Mutex.h>

namespace ne
{
    typedef pthread_t ThreadID;

    class NEAPI Thread
    {
        public:
            Thread(void *(*startRoutine)(void *), void *arg, const pthread_attr_t *attr=0);

            ThreadID getID() const;

            bool joinable() const;

            bool isRunning() const;

            bool isFinished() const;

            bool equal(const Thread &thread) const;

            bool detach();

            bool join(void **threadReturn);

            bool kill(int returnValue);

            /*
            bool cancel();

            // TODO: Only affect the thread when current thread is this thread
            void exit(void *returnValue);
            */

            ~Thread();

        private:
            Thread();

            Thread(const Thread &thread);

            Thread& operator = (const Thread &thread);

        private:
            static void* _Routine(void *arg);

        private:
            mutable Mutex mMutex;
            ThreadID mThread;
            volatile bool mIsJoinable;
            volatile bool mIsRunning;
            volatile bool mIsFinished;
            void *(*mRoutine)(void *);
            void *mArg;
    };
}

#endif
